#include "IAP.h"

//--------------------------------------------------//
//	BOOT引导程序	|	0x8000000-0x8001400	|	5K	//
//--------------------------------------------------//
//	USER功能程序	|	0x8001400-0x8008000	|	27K	//
//--------------------------------------------------//
//	USER升级程序	|	0x8008000-0x800EC00	|	27K	//
//--------------------------------------------------//
//	USER储存空间	|	0x800EC00-0x8010000	|	5K	//
//--------------------------------------------------//
#define TotalFlashSize	0x8010000
#define RemoteDataSaveAddr	0x8008000
#define RemoteUpdataParaSaveAddr	0x800EC00

#define NoUpdata	0	//没有升级
#define Updataing	1	//正在升级
#define UpdataClt	2	//升级完成
#define UpdataErr	4	//升级错误
#define Updataed	5	//已升级

unsigned char  IsUpdata = NoUpdata;
unsigned int   UpdataFileSize = 0;
unsigned short UpdataPacksCount = 0;
unsigned short NextUpdataPackNum = 0;
unsigned short ThisUpdataPackSize = 0;
unsigned char  ReTryAskMessageCount = 0;

//检测升级状态，系统初始化完毕之后启动检测

unsigned char CheckUpdataStatus(void)
{	
	Updata_FlashRead(RemoteUpdataParaSaveAddr,(unsigned short*)IsUpdata,1);
	return IsUpdata;
}

//升级报文检测主函数
//报文格式: | AA 55 | 00 00 80 00 | 00 21 | 00 12 | 02 00 | 00.......02 | 25 | 55 AA |
//报文说明: | 包头  | 升级程序大小| 总包数| 包序号| 包长度| 升级程序数据|校验| 包尾  |

//返回响应: | 55 AA | 00 | 00 13 | 23 | AA 55 |
//返回说明: | 包头  |结果| 包序号|校验| 包尾  |

unsigned char UpdataMain(unsigned char *input,unsigned short length)
{
	//校验数据
	if(Updata_CheckRemoteData(input,length))
	{
		ReTryAskMessageCount=0;
		
	}
	else
	{
		//校验数据无效，发送重发指令
		ReTryAskMessageCount++;
		if(ReTryAskMessageCount > 3)
		{
			//升级过程发生错误，中止升级程序
		}
	}
}